#include <stdio.h>
#include <stdint.h>

uint16_t g_num = 0xDFA8;
uint32_t g_old = 0xEADF9800;
uint32_t g_new = 0xCEFAD200;

void func(uint8_t *num, uint32_t old, uint32_t new) {
  int32_t x;
  uint16_t newTmp, oldTmp;

  x = (num[0] << 8) + num[1];
  x = (~(uint32_t)x) & 0xfffful;

  newTmp = (uint16_t)(new & 0xffff);
  oldTmp = (uint16_t)(old & 0xffff);
  x += (newTmp - oldTmp);
  if (x < 0) {
     x--;
     x = (uint32_t)x & 0xffff;
  } else if ((uint32_t)x & 0x10000) {
     x++;
     x = (uint32_t)x & 0xffff;
  }

  newTmp = (uint16_t)(new >> 16);
  oldTmp = (uint16_t)(old >> 16);
  x += (newTmp - oldTmp);
  if (x < 0) {
     x--;
  } else if ((uint32_t)x & 0x10000) {
     x++;
  }
  
  x = (~(uint32_t)x) & 0xfffful;
  
  num[0] = (uint8_t)((uint16_t)x >> 8);
  num[1] = (uint8_t)x;

  return;
}

int main() {
  func((uint8_t *)&(g_num), g_old, g_new);
  printf("%u\n", g_num);
  return 0;
}
